package org.hank.compose_yyzs.ui.theme

import android.app.Activity
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.TweenSpec
import androidx.compose.material3.ColorScheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.Stable
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.core.view.WindowCompat
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import com.orhanobut.logger.Logger

private val DarkColorPalette = AppColors(
    themeUi = black1,
    background = black2,
    listItem = black3,
    divider = black4,
    textPrimary = white4,
    textSecondary = grey1,
    mainColor = white,
    card = white1,
    icon = white4,
    info = info,
    warn = warn,
    success = green3,
    error = red2,
    primaryBtnBg = blueColor,
    secondBtnBg = white1,
    hot = red,
    placeholder = grey1,
    dialogOutSide = black8,
)

//白天主题
private val LightColorPalette = AppColors(
    themeUi = blueColor,
    background = white2,
    listItem = white,
    divider = white3,
    textPrimary = black3,
    textSecondary = grey1,
    mainColor = white,
    card = white1,
    icon = white4,
    info = info,
    warn = warn,
    success = green3,
    error = red2,
    primaryBtnBg = blueColor,
    secondBtnBg = white3,
    hot = red,
    placeholder = white3,
    dialogOutSide = black8
)
var LocalAppColors = staticCompositionLocalOf {
    LightColorPalette
}

@Stable
object AppTheme {
    val colors: AppColors
        @Composable
        get() = LocalAppColors.current

    enum class Theme {
        Light, Dark
    }
}

@Stable
class AppColors(
    themeUi: Color,
    background: Color,
    listItem: Color,
    divider: Color,
    textPrimary: Color,
    textSecondary: Color,
    mainColor: Color,
    card: Color,
    icon: Color,
    info: Color,
    warn: Color,
    success: Color,
    error: Color,
    primaryBtnBg: Color,
    secondBtnBg: Color,
    hot: Color,
    placeholder: Color,
    dialogOutSide : Color
) {
    var themeUi: Color by mutableStateOf(themeUi)
        internal set
    var background: Color by mutableStateOf(background)
        private set
    var listItem: Color by mutableStateOf(listItem)
        private set
    var divider: Color by mutableStateOf(divider)
        private set
    var textPrimary: Color by mutableStateOf(textPrimary)
        internal set
    var textSecondary: Color by mutableStateOf(textSecondary)
        private set
    var mainColor: Color by mutableStateOf(mainColor)
        internal set
    var card: Color by mutableStateOf(card)
        private set
    var icon: Color by mutableStateOf(icon)
        private set
    var info: Color by mutableStateOf(info)
        private set
    var warn: Color by mutableStateOf(warn)
        private set
    var success: Color by mutableStateOf(success)
        private set
    var error: Color by mutableStateOf(error)
        private set
    var primaryBtnBg: Color by mutableStateOf(primaryBtnBg)
        internal set
    var secondBtnBg: Color by mutableStateOf(secondBtnBg)
        private set
    var hot: Color by mutableStateOf(hot)
        private set
    var placeholder: Color by mutableStateOf(placeholder)
        private set
    var dialogOutSide: Color by mutableStateOf(dialogOutSide)
        private set
}


@Composable
fun AppTheme(
    isDarkMode: Boolean = false,
    content: @Composable () -> Unit
) {
    val targetColors = if (!isDarkMode)
        LightColorPalette
    else
        DarkColorPalette


    val themeUi = animateColorAsState(targetColors.themeUi, TweenSpec(600))
    val background = animateColorAsState(targetColors.background, TweenSpec(600))
    val listItem = animateColorAsState(targetColors.listItem, TweenSpec(600))
    val divider = animateColorAsState(targetColors.divider, TweenSpec(600))
    val textPrimary = animateColorAsState(targetColors.textPrimary, TweenSpec(600))
    val textSecondary = animateColorAsState(targetColors.textSecondary, TweenSpec(600))
    val mainColor = animateColorAsState(targetColors.mainColor, TweenSpec(600))
    val card = animateColorAsState(targetColors.card, TweenSpec(600))
    val icon = animateColorAsState(targetColors.icon, TweenSpec(600))
    val info = animateColorAsState(targetColors.info, TweenSpec(600))
    val warn = animateColorAsState(targetColors.warn, TweenSpec(600))
    val success = animateColorAsState(targetColors.success, TweenSpec(600))
    val error = animateColorAsState(targetColors.error, TweenSpec(600))
    val primaryBtnBg = animateColorAsState(targetColors.primaryBtnBg, TweenSpec(600))
    val secondBtnBg = animateColorAsState(targetColors.secondBtnBg, TweenSpec(600))
    val hot = animateColorAsState(targetColors.hot, TweenSpec(600))
    val placeholder = animateColorAsState(targetColors.placeholder, TweenSpec(600))
    var dialogOutSide = animateColorAsState(targetColors.dialogOutSide, TweenSpec(600))
    val appColors =
        AppColors(
            themeUi = themeUi.value,
            background = background.value,
            listItem = listItem.value,
            divider = divider.value,
            textPrimary = textPrimary.value,
            textSecondary = textSecondary.value,
            mainColor = mainColor.value,
            card = card.value,
            icon = icon.value,
            primaryBtnBg = primaryBtnBg.value,
            secondBtnBg = secondBtnBg.value,
            info = info.value,
            warn = warn.value,
            success = success.value,
            error = error.value,
            hot = hot.value,
            placeholder = placeholder.value,
            dialogOutSide = dialogOutSide.value,
        )


    val systemUiCtrl = rememberSystemUiController()
    systemUiCtrl.setStatusBarColor(Transparent, !isDarkMode)
    systemUiCtrl.setNavigationBarColor(Transparent, !isDarkMode)
    systemUiCtrl.setSystemBarsColor(Transparent, !isDarkMode)

    CompositionLocalProvider(value = LocalAppColors provides appColors) {
        MaterialTheme(
            colorScheme = lightColorScheme(
                primary = AppTheme.colors.themeUi,
                background = AppTheme.colors.background
            ),
            content = content,
            shapes = MyShapes
        )
    }
}
